package com.zf.bm.common.core.sys.web;//package com.zf.bm.common.core.sys.web;
//
//import com.github.stuxuhai.jpinyin.PinyinException;
//import com.github.stuxuhai.jpinyin.PinyinFormat;
//import com.github.stuxuhai.jpinyin.PinyinHelper;
//import com.google.common.collect.Lists;
//import com.google.common.collect.Maps;
//import com.zf.bm.common.constant.AuthenticConstant;
//import com.zf.bm.common.core.config.ConfigUtils;
//import com.zf.bm.common.core.sys.entity.Role;
//import com.zf.bm.common.core.sys.service.SystemService;
//import com.zf.bm.common.core.sys.service.UserService;
//import com.zf.bm.common.dto.ObjectConvert;
//import com.zf.bm.common.core.sys.dto.RoleTreeDTO;
//import com.zf.bm.common.dto.role.RoleDTO;
//import com.zf.bm.common.utils.Result;
//import com.zf.bm.common.utils.ResultStatus;
//import com.zf.bm.common.utils.UserUtils;
//import com.zf.bm.common.web.BaseController;
//import com.zf.bm.config.ParamConfig;
//import com.zf.bm.vo.RoleVo;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//import java.util.Objects;
//
///**
// * 角色Controller
// */
//@RestController
//@RequestMapping(value = "${adminPath}/sys/role", method = {RequestMethod.GET, RequestMethod.POST})
//public class RoleController extends BaseController {
//
//    @Autowired
//    private SystemService systemService;
//    @Autowired
//    private UserService userService;
//    @Resource
//    private ParamConfig paramConfig;
//
//    @ModelAttribute("role")
//    public Role get(@RequestParam(required = false) Long id) {
//        if (Objects.nonNull(id)) {
//            return systemService.getRole(id);
//        } else {
//            return new Role();
//        }
//    }
//
//    @RequestMapping(value = "list")
//    public Result list() {
//        List<Role> list = systemService.findAllRole();
//        List<RoleDTO> roleDTOList = Lists.newArrayList();
//        for (int i = 0; i < list.size(); i++) {
//            RoleDTO roleDTO = new RoleDTO();
//            ObjectConvert<RoleDTO> objectToDto = new ObjectConvert();
//            Role re = list.get(i);
//            List<Map<String,Integer>> menuMap = systemService.queryByRoleId(re.getId());
//            roleDTO = objectToDto.toDto(re, roleDTO);
//            roleDTO.setMenus(menuMap);
//            roleDTOList.add(roleDTO);
//        }
//        Map<String, Object> resultJson = Maps.newHashMap();
//        resultJson.put("content", roleDTOList);
//        resultJson.put("totalElements", list.size());
//        return new Result(resultJson);
//    }
//
//    @RequestMapping(value = "rolesTree")
//    public Result rolesTree() {
//        List<Role> list = systemService.findAllRole();
//        List<RoleTreeDTO> roleList = Lists.newArrayList();
//        for (Role role : list) {
//            ObjectConvert<RoleTreeDTO> dto = new ObjectConvert();
//            RoleTreeDTO roleTreeDTO = dto.toDto(role, new RoleTreeDTO());
//            roleList.add(roleTreeDTO);
//        }
//
//        Map<String, Object> resultJson = Maps.newHashMap();
//        resultJson.put("data", roleList);
//        return new Result(resultJson);
//    }
//
//
////    @RequiresPermissions("sys:role:edit")
////    @RequestMapping(value = "save")
////    public Result save(@RequestBody RoleVo vo) {
////
////        ObjectConvert<Role> convert = new ObjectConvert();
////        Role role = convert.toObj(vo, new Role());
////        try {
////            role.setEnname(PinyinHelper.convertToPinyinString(vo.getName(), "", PinyinFormat.WITHOUT_TONE));
////        } catch (PinyinException e) {
////            logger.error("汉字转拼音失败:{}", e);
////        }
////        role.setRoleType("assignment");
////        role.setMenuIdList(vo.getMenuIds());
////        if (!UserUtils.getUser().isAdmin() && ConfigUtils.YES.equals(role.getSysData())) {
////            return new Result(ResultStatus.DATA_NOT_POWER);
////        }
////
////        if (!checkName(role.getId(), role.getName())) {
////            return new Result(ResultStatus.FAIL.getCode(),
////                    "保存角色'" + role.getName() + "'失败, 角色名已存在");
////        }
////        systemService.saveRole(role);
////        String name = role.getName();
////        if (Boolean.TRUE.equals(paramConfig.getEnabled())){
////            Role serviceRole = systemService.getRole(role.getId());
////            name = serviceRole.getName();
////        }
////        AuthenticConstant.clear();
////        return new Result("保存角色'" + name + "'成功");
////    }
//
//    @RequiresPermissions("sys:role:edit")
//    @RequestMapping(value = "delete")
//    public Result delete(@RequestBody RoleVo vo) {
//        Long id = vo.getId();
//        Role role = new Role(id).selectById();
//        //当角色下存在用户时不能删除
//        Long userCount = userService.countUserRole(id);
//        if (userCount > 0) {
//            return new Result(ResultStatus.FAIL.getCode(),
//                    "角色下存在用户，请先删除用户！");
//        }
//
//        if (!UserUtils.getUser().isAdmin() && ConfigUtils.YES.equals(role.getIsSys())) {
//            return new Result(ResultStatus.DATA_NOT_POWER.getCode(),
//                    "越权操作，只有超级管理员才能修改此数据！");
//        }
//        systemService.deleteRole(role);
//        AuthenticConstant.clear();
//        return new Result("删除角色成功");
//    }
//
//
//    public boolean checkName(Long id, String name) {
//
//        Role role = systemService.getRoleByName(name);
//        if (name != null && role != null && null == id) {
//            return Boolean.FALSE;
//        } else if (role != null && null != id && id.longValue() != role.getId().longValue()) {
//            return Boolean.FALSE;
//        }
//        return Boolean.TRUE;
//    }
//
//}
